package com.hitqz.robot.listener;

import com.google.common.collect.Table;
import com.hitqz.robot.HitChipService;
import com.hitqz.robot.api.business.event.EquipmentPoseUpdateEvent;
import com.hitqz.robot.api.common.entity.RobotEquipmentPose;
import com.hitqz.robot.camera.communication.NetworkServiceThreadPool;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@Slf4j
@AllArgsConstructor
public class ChipEventListener {

    private final HitChipService chipService;


    @EventListener
    public void onUpdatePoseEvent(EquipmentPoseUpdateEvent event){
        NetworkServiceThreadPool.dealEvent(()->{
            if (!Objects.equals(event.getBrand().toUpperCase(), chipService.brand().getEnStr().toUpperCase())){
                return;
            }
            //删除旧值
            log.info("[chip]更新自定义姿态:{}",event);
            String ip = event.getIp();
            Table<String, String, String> paramsCacheTable = chipService.getParamsCacheTable();
            paramsCacheTable.rowMap().remove(ip);
            //缓存新的值
            for (RobotEquipmentPose pose : event.getPoseList()) {
                paramsCacheTable.put(ip,pose.getTypeStr(),pose.getJson());
            }
        });
    }

}
